package com.itheima.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MQConfig {

    @Autowired
    //给RabbitTemplate设置回执(路由队列失败)
    private RabbitTemplate rabbitTemplate;


    /**
     * 当前类完成初始化后,会执行该注解所有的方法
     */
    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {

            /**
             * 消息发送交换机,路由队列失败,这里收到消息
             *
             * @param returned
             */
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                Message message = returned.getMessage();
                String exchange = returned.getExchange();
                String routingKey = returned.getRoutingKey();
                int replyCode = returned.getReplyCode();
                String replytext = returned.getReplyText();

                log.info("message:" + message);
                log.info("exchange:" + exchange);
                log.info("routingKey:" + routingKey);
                log.info("relyCode:" + replyCode);
                log.info("relyText:" + replytext);
            }
        });
    }
}